/*:
* @plugindesc (v1.02)裝備作為物品使用
* @author 小c
* @version 1.0.2
* @date 5/27/2020
*
* @help
* 該插件實現裝備作為物品使用
*
* 武器/防具標籤
* <Equip Use Item: x>
* 使用此裝備的效果為物品x，x為物品編號
* 裝備的使用條件由物品x決定
* 裝備的效果【包括使用完畢後是否消耗】由物品x決定
* 注意：
* 1. 物品x僅作為裝備的使用效果的標記，僅有標記作用，不要讓它成為實際的
* 道具參與遊戲進程
* 2. 戰鬥中使用裝備是會顯示物品x的名稱與圖示。
*
* 更新日誌
* v1.00
* 插件完成
*
* v1.01
* 追加讀取行動裝備的函數
*
* v1.02
* 解決裝備在戰鬥中使用不消耗的問題
*/

var Imported = Imported || {};
Imported.CP_UseEquip = true;

var CP = CP || {};
CP.UseEquipManager = CP.UseEquipManager || {};

//-------------------------
//  DataManager
//-------------------------
CP.UseEquipManager.DATABASE_LOADED = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function(){
	if(!CP.UseEquipManager.DATABASE_LOADED.call(this))
		return false;

	CP.UseEquipManager.loadEquipUseData($dataWeapons);
	CP.UseEquipManager.loadEquipUseData($dataArmors);

	return true;
};

CP.UseEquipManager.loadEquipUseData = function(array){
	this.loadEquipUseBaseData(array);
	this.loadEquipUseItemData(array);
};

CP.UseEquipManager.loadEquipUseBaseData = function(array){
	for(var i = 1; i < array.length; i++){
		var equip = array[i];
		equip.useItemId = 0;

		if(equip.meta["Equip Use Item"])
			equip.useItemId = Number(equip.meta["Equip Use Item"]) || 0;
	}
};

CP.UseEquipManager.loadEquipUseItemData = function(array){
	for(var i = 1; i < array.length; i++){
		var equip = array[i];
		equip.consumable = false;

		if(equip.useItemId > 0){
			var item = this.getRealUseItem(equip);
			equip.consumable = item.consumable;
			item.isUseableEquip = true;
		}
	}
};

//針對裝備，是否是【可使用的裝備】，非裝備返回false
CP.UseEquipManager.isEquipUsable = function(item){
	if(!item)
		return false;
	if(DataManager.isSkill(item) || DataManager.isItem(item))
		return false;
	if((DataManager.isWeapon(item) || DataManager.isArmor(item)) && item.useItemId > 0)
		return true;
	return false;
};

CP.UseEquipManager.getRealUseItem = function(item){
	if(!item)
		return null;

	if(DataManager.isItem(item) || DataManager.isSkill(item))
		return item;

	if(item.useItemId > 0)
		return $dataItems[item.useItemId];

	return null;
};

//是否是裝備指向的標記物品
CP.UseEquipManager.isUseableEquip = function(item){
	if(item.isUseableEquip)
		return true;
	else
		return false;
};

//-------------------------
//  Game_BattlerBase
//-------------------------
CP.UseEquipManager.CAN_USE_ITEM = Game_BattlerBase.prototype.canUse;
Game_BattlerBase.prototype.canUse = function(item){
	return CP.UseEquipManager.CAN_USE_ITEM.call(this, CP.UseEquipManager.getRealUseItem(item));
};

//-------------------------
//  Game_Battler
//-------------------------
CP.UseEquipManager.BATTLER_USE_ITEM = Game_Battler.prototype.useItem;
Game_Battler.prototype.useItem = function(item){
    if(CP.UseEquipManager.isEquipUsable(item))
        this.consumeItem(item);
    else
    	CP.UseEquipManager.BATTLER_USE_ITEM.call(this, item);
};

//-------------------------
//  Game_Party
//------------------------
CP.UseEquipManager.PARTY_CONSUME_ITEM = Game_Party.prototype.consumeItem;
Game_Party.prototype.consumeItem = function(item){
    if (CP.UseEquipManager.isEquipUsable(item) && item.consumable)
        this.loseItem(item, 1);
    else
    	CP.UseEquipManager.PARTY_CONSUME_ITEM.call(this, item);
};

//因物品不存在時判定為不可用，故所有虛擬物品均判定為玩家持有
CP.UseEquipManager.PARTY_HAS_ITEM = Game_Party.prototype.hasItem;
Game_Party.prototype.hasItem = function(item, includeEquip){
	return CP.UseEquipManager.PARTY_HAS_ITEM.call(this, item, includeEquip) || CP.UseEquipManager.isUseableEquip(item);
};

//-------------------------
//  Game_Action
//------------------------
CP.UseEquipManager.CLEAR_ACTION = Game_Action.prototype.clear;
Game_Action.prototype.clear = function(){
	CP.UseEquipManager.CLEAR_ACTION.call(this);
	this.setEquipItem(null);
};

Game_Action.prototype.setEquipItem = function(item){
	this._equipItem = item;
};

Game_Action.prototype.equipItem = function(){
	return this._equipItem;
};

CP.UseEquipManager.SET_ACTION_SKILL = Game_Action.prototype.setSkill;
Game_Action.prototype.setSkill = function(skillId) {
    CP.UseEquipManager.SET_ACTION_SKILL.call(this, skillId);
    this.setEquipItem(null);
};

CP.UseEquipManager.SET_ACTION_ITEM = Game_Action.prototype.setItem;
Game_Action.prototype.setItem = function(itemId) {
    CP.UseEquipManager.SET_ACTION_ITEM.call(this, itemId);
    this.setEquipItem(null);
};

CP.UseEquipManager.SET_ACTION_ITEM_OBJECT = Game_Action.prototype.setItemObject;
Game_Action.prototype.setItemObject = function(object){
	if(CP.UseEquipManager.isEquipUsable(object))
		this.setEquipItem(object);
	else
		this.setEquipItem(null);
    CP.UseEquipManager.SET_ACTION_ITEM_OBJECT.call(this, CP.UseEquipManager.getRealUseItem(object));
};

//-------------------------
//  Scene_Battle
//-------------------------
CP.UseEquipManager.BATTLE_ON_ITEM_OK = Scene_Battle.prototype.onItemOk;
Scene_Battle.prototype.onItemOk = function(){
	var item = this._itemWindow.item();
    if(DataManager.isWeapon(item) || DataManager.isArmor(item))
    	this.onItemOkForEquip();
    else
    	CP.UseEquipManager.BATTLE_ON_ITEM_OK.call(this);
};

Scene_Battle.prototype.onItemOkForEquip = function() {
    var item = this._itemWindow.item();
    var action = BattleManager.inputtingAction();
    action.setItemObject(item);
    $gameParty.setLastItem(item);
    this.onSelectAction();
};

//-------------------------
//  BattleManager
//-------------------------
//戰鬥中指向正確的物品來消耗
CP.UseEquipManager.BATTLE_START_ACTION = BattleManager.startAction;
BattleManager.startAction = function(){
	CP.UseEquipManager.BATTLE_START_ACTION.call(this);

	var action = this._subject.currentAction();
	if(action.equipItem())
		this._subject.useItem(action.equipItem());
};